package cn.bytecollege.admin.dao;

import cn.bytecollege.common.dao.BaseDao;
import cn.bytecollege.common.entity.Classify;
import java.util.List;
import cn.bytecollege.common.util.DBHelper;

import java.util.List;

/**
 * @Auther：MR.P
 * @Date:2021/10/28 10 11
 * @Description:
 */
public class ClassifyDao extends BaseDao<Classify,Integer> {
    private DBHelper helper = null;
    public ClassifyDao(){
        helper = new DBHelper();
    }

    /**
     * 添加分类
     * @param classify
     * @return
     */
    public int save(Classify classify){
        String sql = "insert into classify (classify_name) values(?) ";
        return helper.executeUpdate(sql,classify.getClassifyName());
    }

    /**
     *根据分类名字查询分类
     * @param classifyName
     * @return
     */
    public Classify findByClassifyName(String classifyName){
        String sql = "select classify_name from classify where classify_name = ? ";
        List<Classify> list = helper.executeQuery(sql,Classify.class,classifyName);
        Classify classify = null;
        if(list.size() > 0){
            classify = list.get(0);
        }
        return classify;
    }

    /**
     * 分页查询
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public List<Classify> findByPage(int pageIndex,int pageSize){
        String sql = "select classify_id,classify_name from classify limit ?,? ";
        int index  = (pageIndex-1)*pageSize;
        return helper.executeQuery(sql,Classify.class,index,pageSize);
    }

    /*
    * 查询所有条数
    * */
    public int count(){
        String sql = "select count(*) from classify";
        return helper.executeScale(sql);
    }

    /**
     * 查询所有的分类
     * @return
     */
    public List<Classify> find(){
        String sql ="select classify_id,classify_name from classify";
        return helper.executeQuery(sql,Classify.class);
    }

    /**
     * 根据分类的id找出分类
     * @param classifyId
     * @return
     */
    public Classify findById(Integer classifyId){
        String sql = "select classify_id,classify_name from classify where classify_id = ? ";
        return helper.executeQuery(sql,Classify.class,classifyId).get(0);
    }

    /**
     * 修改分类
     * @param classify
     * @return
     */
    public int update(Classify classify){
        String sql = "update classify set classify_name = ? where classify_id = ?";
        return helper.executeUpdate(sql,classify.getClassifyName(),classify.getClassifyId());
    }
    /**
     * 添加商品时，商家可以下拉看有哪些商品分类，此方法就是查出商品分类。
     * 这方法不是重写的。
     * @return
     */
    public List<Classify> findClassify(){
        String sql = "select classify_id,classify_name from classify";
        return helper.executeQuery(sql,Classify.class);
    }

    /**
     * 根据分类id删除分类
     * @param classifyId
     * @return
     */
    public int deleteClassifyById(Integer classifyId){
        String sql = "delete from classify where classify_id = ?";
        return helper.executeUpdate(sql,classifyId);
    }

    /**
     * 根据分类的名字找出分类,分页
     * @param classifyName
     * @return
     */
    public List<Classify> findListByClassifyName(String classifyName,int pageIndex,int pageSize){
        String sql = "select classify_id,classify_name from classify where classify_name like '%' ? '%' limit ?,?";
        int index  = (pageIndex-1)*pageSize;
        Object [] objects = {classifyName,index,pageSize};

        return helper.executeQuery(sql,Classify.class,objects);
    }

    /**
     * 模糊查询相应的条数
     * @param classifyName
     * @return
     */
    public int countByClassifyName(String classifyName){
        String sql = "select count(*) from classify where classify_name like '%' ? '%'";
        return helper.executeScale(sql,classifyName);
    }
}
